package com.astruyk.chordophone;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;

public class Chordophone extends Activity {
	private List<String> _variants = new ArrayList<String>();
	private ChordListAdapter _adapter;
	private String _filterChordName;
    private String _filterVariantName;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        List<Chord> chords;
        try{
        	chords = (new ChordImporter().importChords(getResources().openRawResource(R.raw.chords)));
        }catch(Exception ex){
        	Log.e("Chordophone", "Error importing chords.", ex);
        	chords = new ArrayList<Chord>();
        }
        
        //Setup the list of all variants
        //TODO populate the variant list based on the available set of variants for the filtered list only..
        _variants.add("All");
        for(Chord c : chords){
        	if(!_variants.contains(c.getVariant())){
        		_variants.add(c.getVariant());
        	}
        }
        
        _adapter = new ChordListAdapter(this, chords);

        //Setup the list of chords
        ListView view = (ListView) findViewById(R.id.chord_list);
        view.setOnItemClickListener(new OnItemClickListener(){
        	@Override
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        		ChordView chordView = (ChordView)findViewById(R.id.chord_display);
        		chordView.setChord((Chord)_adapter.getItem(position));
        	}
        });
        view.setAdapter(_adapter);
        view.setTextFilterEnabled(true);
        
        //Setup the chord filter spinner
        Spinner chordSpinner = (Spinner) findViewById(R.id.filter_spinner);
        ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(
        		this,
        		R.array.chord_filters,
        		android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        chordSpinner.setAdapter(adapter);
        chordSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
			@Override
			public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) {
				if(pos == 0) {
					//This is the "Any" value - don't filter by this criteria
					_filterChordName = "";
				}else{
					_filterChordName = getResources().getStringArray(R.array.chord_filters)[pos];
				}
				RefilterItems();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				//Do nothing
			}
        });
        
        //Setup the variant filter spinner
        Spinner variantSpinner = (Spinner) findViewById(R.id.variant_spinner);
        adapter = new ArrayAdapter<String>(
        		this,
        		android.R.layout.simple_spinner_item,
        		_variants);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        variantSpinner.setAdapter(adapter);
        variantSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
			@Override
			public void onItemSelected(AdapterView<?> parent, View v, int pos, long row) {
				if(pos == 0) {
					//This is the "Any" value - don't filter by this criteria
					_filterVariantName = "";
				}else{
					_filterVariantName = _variants.get(pos);
				}
				RefilterItems();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				//Do nothing
			}
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu){
    	getMenuInflater().inflate(R.menu.main_menu, menu);
    	return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu){
    	MenuItem onlyShowCommonMenuItem = menu.findItem(R.id.menu_common_only);
    	if(getShowSimpleOnly()){
    		onlyShowCommonMenuItem.setTitle(getResources().getString(R.string.menu_all_chords));
    	}else{
    		onlyShowCommonMenuItem.setTitle(getResources().getString(R.string.menu_common_only));
    	}
    	
    	return super.onPrepareOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	switch (item.getItemId()) {
        case R.id.menu_common_only:
        	boolean oldValue = getShowSimpleOnly();
        	setShowSimpleOnly(!oldValue);
        	return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    private static final String ONLY_SIMPLE_PREFERENCE_NAME = "onlySimple";
    
    private boolean getShowSimpleOnly(){
    	return getPreferences(MODE_PRIVATE).getBoolean(ONLY_SIMPLE_PREFERENCE_NAME, true);
    }
    
    private void setShowSimpleOnly(boolean simpleOnly){
    	SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
    	editor.putBoolean(ONLY_SIMPLE_PREFERENCE_NAME, simpleOnly);
    	editor.commit();
    	RefilterItems();
    }
    
    private void RefilterItems(){
    	_adapter.getFilter().filter(ChordListAdapter.buildFilterConstraint(_filterChordName, _filterVariantName, getShowSimpleOnly()));
    }
}